package com.rocksea.app.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;
import java.util.UUID;

/**
 * Created by 吴志华 on 2017/5/4.
 * 文件读写工具类
 */

public class FileHelper {
    private FileOutputStream fileOutputStream = null;

    public FileHelper()
    {}
    /**
     * 保存图片
     * */
    public String writePhoto(Bitmap bitmap,String sectId,String siteId,int inspectionFormId)
    {
        String photoName = String.format("%s.png", UUID.randomUUID().toString());
        String paths = String.format("/%s/%s/%d",sectId,siteId,inspectionFormId);

        File dirs = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/upImages"+paths);
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/upImages"+paths,String.format("%s.jpg",photoName));
        String path = Environment.getExternalStorageDirectory().getPath();
        System.out.println(path);
        try {
            if (dirs.exists()) {
                return doWrite(bitmap, fileOutputStream, file);
            }else {
                if (dirs.mkdirs()){
                   return doWrite(bitmap, fileOutputStream, file);
                }else {
                    Log.d("onActivityResult: ","未创建成功文件夹");
                    return null;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    private String doWrite(Bitmap bitmap, FileOutputStream fileOutputStream, File file) throws IOException {
        if (file.createNewFile())
        {
            try
            {
                fileOutputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG,100, fileOutputStream);
                fileOutputStream.flush();
                return file.getPath();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                return null;
            }
            finally
            {
                try
                {
                    if (fileOutputStream!=null)
                        fileOutputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        else
        {
            return file.getPath();
        }
    }

    public String writeBitmap(Bitmap bitmap,String fileName)
    {
        File dirs = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/temp");
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/temp",fileName);
        String path = Environment.getExternalStorageDirectory().getPath();
        System.out.println(path);
        try
        {
            if (dirs.exists())
            {
                return doWrite(bitmap, fileOutputStream, file);
            }
            else
            {
                if (dirs.mkdirs())
                    return doWrite(bitmap, fileOutputStream, file);
                else
                {
                    Log.d("onActivityResult: ","未创建成功文件夹");
                    return null;
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }



    }

    public void wirteFile(String fileName,String text)
    {
        File dirs = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/json");
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/json",String.format("%s.txt",fileName));
        if (dirs.exists()) {
            wirteText(text,file);
        }else
        {
            if (dirs.mkdirs()) {
                wirteText(text,file);
            }
        }
    }

    public void wirteCrash(String fileName,String text)
    {
        File dirs = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/crash");
        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/crash",String.format("%s",fileName));
        if (dirs.exists()) {
            wirteText(text,file);
        }else
        {
            if (dirs.mkdirs()) {
                wirteText(text,file);
            }
        }
    }


    private void wirteText(String text,File file)
    {
        try
        {
            RandomAccessFile accessFile = new RandomAccessFile(file,"rwd");
            accessFile.seek(file.length());
            accessFile.write(text.getBytes());
            accessFile.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 保存本地现场照片
     * @param bitmap 需要保存的bitmap
     *
     * @return  返回保存的路径
     * <P>
     *
     * </P>
     * */
    public static String  writeBitMap(Bitmap bitmap,String fileName)
    {
        File filedir = new File(Environment.getExternalStorageDirectory().getPath()+"/ChinaRail/Image");
        if (!filedir.exists())
        {
            if (filedir.mkdirs())
            {
                Log.e("drawSonicWaveBitmap: ","创建文件夹成功" );
            }
            else
            {
                Log.e("drawSonicWaveBitmap: ","创建文件夹失败" );
                return "";
            }
        }
        File image = new File(filedir,String.format("%s",fileName));
        try
        {
            FileOutputStream os = new FileOutputStream(image);
            if (bitmap.compress(Bitmap.CompressFormat.JPEG,100,os))
            {
                os.flush();
                os.close();
                return image.getPath();
            }
            else
                return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
